package com.enterprise.service.impl;

import com.enterprise.dao.BaseDao;
import com.enterprise.entity.Homedata;
import com.enterprise.entity.User;
import com.enterprise.entity.YearData;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.UserService;
import com.enterprise.util.DaoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 用户业务逻辑实现类
 * 
 * @author huangf
 * 
 */
@Slf4j
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public User login(User user) {
		return (User) dao.selectOne("user.selectOne", user);
	}

	public List selectList(User user) {
		if (user == null)
			return dao.selectList("user.selectList");
		return dao.selectList("user.selectList", user);
	}

	public int insert(User user) {
		Object userid= dao.selectOne("user.getUserid");
		user.setUserid(DaoUtils.getMaxId(userid));
		return dao.insert("user.insert", user);
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 */
	public int deletes(String[] ids) {
		User user = new User();
		for (int i = 0; i < ids.length; i++) {
			user.setId(Integer.parseInt(ids[i]));
			delete(user);
		}
		return 0;
	}

	public int delete(User e) {
		return dao.delete("user.delete", e);
	}

	public int update(User e) {
		return dao.update("user.update", e);
	}

	public PageModel selectPageList(User e) {
		return dao.selectPageList("user.selectPageList",
				"user.selectPageCount", e);
	}

	@Override
	public User selectOne(User e) {
		return (User) dao.selectOne("user.selectOne", e);
	}

	@Override
	public User selectById(int id) {
		User user = new User();
		user.setId(id);
		return selectOne(user);
	}
	
	/**
	 * 根据条件查询数量
	 * @param user
	 * @return
	 */
	public int selectCount(User user) {
		if(user==null){
			throw new NullPointerException();
		}
		
		return (Integer) dao.selectOne("user.selectCount",user);
	}

	public User selectOneByCondition(User user) {
		if(user==null){
			throw new NullPointerException();
		}
		return (User) dao.selectOne("user.selectOneByCondition", user);
	}

	/**
	 *
	 * @return
	 */
	public Homedata selectHomedata() {
		Homedata homedata = (Homedata)dao.selectOne("user.selectHomedata1", new User());
		List<String> mdates = getMdate();
		List<YearData> yearDatas = new ArrayList<>();
		for (String mdate:mdates) {
			YearData yearData = (YearData) dao.selectOne("user.selectHomedata4", mdate);
			yearDatas.add(yearData);
		}
		homedata.setYearDatas(yearDatas);
		return  homedata;
	}

	// 获取今年全部月份的list
	private static List<String> getMdate(){
		List<String> mdate = new ArrayList<>();
		Calendar date = Calendar.getInstance();
		String year = String.valueOf(date.get(Calendar.YEAR));
		for (int i =1;i<10;i++){
			mdate.add(year+"-0"+i);
		}
		mdate.add(year+"-10");
		mdate.add(year+"-11");
		mdate.add(year+"-12");
		return mdate;
	}

}
